﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LSQL;

namespace Shopfloormanagement
{
    public partial class Key_Calendar : Form
    {
        //lab_eariler 查询当年的过往记录 计数
        //lab_num1当周事件数  lab_cw当周周号  lab_num2下周事件数  lab_cw2下周周号
        //lab_forthcoming当年内未来事件数
        //btn_addtopic 弹出填写事件框  btn_delete  删除选中的事件
        public static Form keyCalendar = new Key_Calendar();
        public static Key_Calendar frm = new Key_Calendar();
        public Key_Calendar()
        {
            keyCalendar = this;
            InitializeComponent();
        }
        
        private void InitAllControls()
        {

            lab_cw.Text = "CW";
            lab_cw2.Text = "CW";

            lab_num1.Text = "0";
            lab_num2.Text = "0";
            lab_eariler.Text = "0";
            lab_forthcoming.Text = "0";
        }

        #region 查询数据

        private string base_query = $"SELECT [ID],[Key_calendar],[Due_date],[Time],[FullName],[Create_time] FROM [{SystemInfo.db}].[dbo].[View_KeyCalendar] ";

        private int[] Count_calendar()
        {
            int[] count = { 0, 0, 0, 0 };

            

            string condition = @"select
	                            COUNT(CASE WHEN DATEPART(WK,Due_date) = DATEPART(WK,GETDATE()) THEN 1 END) AS thisweek,
	                            COUNT(CASE WHEN DATEPART(WK,Due_date) < DATEPART(WK,GETDATE()) THEN 1 END) AS preweek,
	                            COUNT(CASE WHEN DATEPART(WK,Due_date) = DATEPART(WK,GETDATE()) + 1 THEN 1 END) AS nextweek,
	                            COUNT(CASE WHEN DATEPART(WK,Due_date) > DATEPART(WK,GETDATE() + 1) THEN 1 END) AS forthweek
	                            from dbo.Key_calendar
	                            where DATEPART(year,Due_date) = DATEPART(year,GETDATE()) and Del_tag = 0";

           
            DataTable dt = SQL.ReturnDataTable(SystemInfo.SQL,condition);
            

            if (dt.Rows != null)
            {
                count[0] = (int)dt.Rows[0]["preweek"];
                count[1] = (int)dt.Rows[0]["thisweek"];
                count[2] = (int)dt.Rows[0]["nextweek"];
                count[3] = (int)dt.Rows[0]["forthweek"];
                return count;
            }
            else
            {
                return count;
            }
        }



        private void Search_Curr()//查当前周
        {
            string condition = "DATEPART(WK,Due_date) = DATEPART(WK,GETDATE()) and DATEPART(YEAR, Due_date) = DATEPART(YEAR, GETDATE()) order by Due_date desc";
            
            DataTable dt = SQL.ReturnDataTable3(SystemInfo.SQL,"View_KeyCalendar",condition);
            

            if (dt.Rows != null)
            {
                dgv_Basic.DataSource = dt;

                for (int i = 0; i < dgv_Basic.RowCount; i++)
                {
                    dgv_Keycalendar.Rows.Add();
                    for (int j = 1; j < dgv_Basic.ColumnCount; j++)
                    {
                        dgv_Keycalendar.Rows[i].Cells[j - 1].Value = dgv_Basic.Rows[i].Cells[j].Value;
                    }
                }
            }
            pic_Curr.BackColor = Color.LimeGreen;
            pic_Forthcoming.BackColor = Color.White;
            pic_Nextweek.BackColor = Color.White;
            pic_History.BackColor = Color.White;
        }

        private void Search_Next()//查下周
        {
            

            string condition = "DATEPART(WK,Due_date) = DATEPART(WK,GETDATE()) + 1 and DATEPART(YEAR, Due_date) = DATEPART(YEAR, GETDATE()) order by Due_date desc";
            DataTable dt = SQL.ReturnDataTable3(SystemInfo.SQL, "View_KeyCalendar", condition);

            if (dt.Rows != null)
            {
                dgv_Basic.DataSource = dt;

                for (int i = 0; i < dgv_Basic.RowCount; i++)
                {
                    dgv_Keycalendar.Rows.Add();
                    for (int j = 1; j < dgv_Basic.ColumnCount; j++)
                    {
                        dgv_Keycalendar.Rows[i].Cells[j - 1].Value = dgv_Basic.Rows[i].Cells[j].Value;
                    }
                }
            }
            pic_Nextweek.BackColor = Color.LimeGreen;
            pic_Forthcoming.BackColor = Color.White;
            pic_Curr.BackColor = Color.White;
            pic_History.BackColor = Color.White;
        }

        private void Search_History()//查之前数据
        {
            string condition = "DATEPART(WK,Due_date) < DATEPART(WK,GETDATE()) and DATEPART(YEAR, Due_date) = DATEPART(YEAR, GETDATE()) order by Due_date desc";

            DataTable dt = SQL.ReturnDataTable3(SystemInfo.SQL, "View_KeyCalendar", condition);

            if (dt.Rows != null)
            {
                dgv_Basic.DataSource = dt;

                for (int i = 0; i < dgv_Basic.RowCount; i++)
                {
                    dgv_Keycalendar.Rows.Add();
                    for (int j = 1; j < dgv_Basic.ColumnCount; j++)
                    {
                        dgv_Keycalendar.Rows[i].Cells[j - 1].Value = dgv_Basic.Rows[i].Cells[j].Value;
                    }
                }
            }
            pic_History.BackColor = Color.LimeGreen;
            pic_Curr.BackColor = Color.White;
            pic_Nextweek.BackColor = Color.White;
            pic_Forthcoming.BackColor = Color.White;
        }

        private void Search_Forthcoming()//查除下周外未来数据
        {
           

            string condition = "DATEPART(WK,Due_date) > DATEPART(WK,GETDATE()) + 1 and DATEPART(YEAR, Due_date) = DATEPART(YEAR, GETDATE()) order by Due_date desc";
            
            DataTable dt = SQL.ReturnDataTable3(SystemInfo.SQL,"View_KeyCalendar", condition);

            if (dt.Rows != null)
            {
                dgv_Basic.DataSource = dt;

                for (int i = 0; i < dgv_Basic.RowCount; i++)
                {
                    dgv_Keycalendar.Rows.Add();
                    for (int j = 1; j < dgv_Basic.ColumnCount; j++)
                    {
                        dgv_Keycalendar.Rows[i].Cells[j - 1].Value = dgv_Basic.Rows[i].Cells[j].Value;
                    }
                }
            }
            pic_Forthcoming.BackColor = Color.LimeGreen;
            pic_Curr.BackColor = Color.White;
            pic_Nextweek.BackColor = Color.White;
            pic_History.BackColor = Color.White;
        }

        #endregion 查询数据

        private void Key_Calendar_Load(object sender, EventArgs e)
        {
            frm = this;
            InitAllControls();
            int[] Count_num = Count_calendar();

            lab_eariler.Text = Count_num[0].ToString();
            lab_num1.Text = Count_num[1].ToString();
            lab_num2.Text = Count_num[2].ToString();
            lab_forthcoming.Text = Count_num[3].ToString();

            // 获取特定文化的 Calendar 对象，这里使用的是 ISO 8601 标准的周号计算方法
            CultureInfo cultureInfo = CultureInfo.CurrentCulture;
            Calendar calendar = cultureInfo.Calendar;

            // 获取一周的第一天（通常在 ISO 8601 标准中，星期一是第一天）
            CalendarWeekRule calendarWeekRule = cultureInfo.DateTimeFormat.CalendarWeekRule;
            DayOfWeek firstDayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;

            // 计算周号
            int weekNumber = calendar.GetWeekOfYear(DateTime.Now, calendarWeekRule, firstDayOfWeek);
            int nextweek = weekNumber + 1;
            lab_cw.Text = "CW" + weekNumber;
            lab_cw2.Text = "CW" + nextweek;

            //设置datagridview
            dgv_Keycalendar.ForeColor = Color.Black;

            Search_Curr();
        }

        private void lab_eariler_Click(object sender, EventArgs e)
        {
            dgv_Keycalendar.Rows.Clear();
            Search_History();
        }

        private void lab_num1_Click(object sender, EventArgs e)
        {
            dgv_Keycalendar.Rows.Clear();
            Search_Curr();
        }

        private void lab_num2_Click(object sender, EventArgs e)
        {
            dgv_Keycalendar.Rows.Clear();
            Search_Next();
        }

        private void lab_forthcoming_Click(object sender, EventArgs e)
        {
            dgv_Keycalendar.Rows.Clear();
            Search_Forthcoming();
        }

        private void btn_addtopic_Click(object sender, EventArgs e)
        {
            Form ad = new Keycalendar_input();
            ad.ShowDialog();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            //定义选择列及拾取ID号
            int idx = dgv_Keycalendar.CurrentRow.Index;
            if (dgv_Keycalendar.CurrentRow == null) return;//排除没有选择行的情况
            int ID = (int)dgv_Basic.Rows[idx].Cells[0].Value;
            //定义pic_name存放为绿色的picturebox的Name, 用于后续switchcase选择要执行的查询函数
            string pic_name = "";
            foreach (Control control in pl_Top.Controls)
            {
                if (control is PictureBox pic)
                {
                    if (pic.BackColor == Color.LimeGreen)
                    {
                        pic_name = pic.Name;
                    }
                }
            }
            //主要业务逻辑,将ID号为选定行ID的信息的del_tag改为1

            bool result = SQL.Update(SystemInfo.SQL, "Key_calendar", "del_tag = 1", $"ID = {ID}");
            if (result)
            {
                MessageBox.Show("已删除" + 1 + "条信息", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            //switchcase选择需要执行的查询函数
            switch (pic_name)
            {
                case "pic_History":
                    dgv_Keycalendar.Rows.Clear();
                    Search_History();
                    break;

                case "pic_Curr":
                    dgv_Keycalendar.Rows.Clear();
                    Search_Curr();
                    break;

                case "pic_Nextweek":
                    dgv_Keycalendar.Rows.Clear();
                    Search_Next();
                    break;

                case "pic_Forthcoming":
                    dgv_Keycalendar.Rows.Clear();
                    Search_Forthcoming();
                    break;

                default:
                    break;
            }
            InitAllControls();
            int[] Count_num = Count_calendar();

            lab_eariler.Text = Count_num[0].ToString();
            lab_num1.Text = Count_num[1].ToString();
            lab_num2.Text = Count_num[2].ToString();
            lab_forthcoming.Text = Count_num[3].ToString();
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            DevMainForm.instance.Search_keyCalendar();
            this.Close();
            
        }

        private void dgv_Keycalendar_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //双击打开input窗口，把信息传到input窗口
            int idx = dgv_Keycalendar.CurrentCell.RowIndex;
            Keycalendar_input.instance.EventText = dgv_Keycalendar.CurrentRow.Cells[0].Value.ToString();
            Keycalendar_input.instance.EventDate = dgv_Keycalendar.CurrentRow.Cells[1].Value.ToString();
            Keycalendar_input.instance.EventTime = dgv_Keycalendar.CurrentRow.Cells[2].Value.ToString();
            
            Keycalendar_input.instance.EventID = dgv_Basic.Rows[idx].Cells[0].Value.ToString();
            Keycalendar_input.instance.btn_Save.Text = "Save Edit";

            //打开窗口
            Keycalendar_input.instance.ShowDialog();
            dgv_Keycalendar.Rows.Clear();
            Search_Curr();

        }
    }
}